
import { App } from "../../application";
import DomUtils from "../../utils/DomUtils";
import Helper from "../../utils/Helper";
import { Widget } from "./Widget";

class Button extends Widget{
    public dom;
    public name;
    public click;
    private app: App;
    constructor(name, click) {
        super();
        this.app = App.getInstance();
        this.name = name ? name : '按钮';
        this.click = click ? click : function () { };
        // <input type="button" value="创建父子树" class="ge-button">
        // <input type="button" value="重置" class="ge-button">

        let scope = this;
        Helper.addPropertyListener(this, 'name', {
            getter: null,
            setter: function (value) {
                scope.dom.value = value;
            }
        })
    }

    public loadToDom() {
        this.dom = DomUtils.create('input');
        this.dom.type = 'button';
        this.dom.value = this.name;
        this.dom.setAttribute('class', 'ge-button');
        let scope = this;
        this.dom.addEventListener('click', function (event) {
            Helper.stopEvents(event);
            scope.click(event);
        },false);

        this.app.ui.buttonWidget.appendChild(this.dom);
    }

    public setButtonValue(value: string){
        this.dom.value = value;
    }

     public dispose(){
        if(!this.dom || !this.dom.parentNode){
            return;
        }
        this.dom.parentNode.removeChild(this.dom)
    }
}
export { Button }